package com.ruoyi.project.men.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.men.domain.MenNewDO;
import com.ruoyi.project.men.service.MenNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 活动专区Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/men/menNew")
public class MenNewController extends BaseController
{
	@Autowired
	private MenNewService menNewService;

	/**
	 * 查询活动专区列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('men:menNew:list')")
	@GetMapping("/list")
	public TableDataInfo list(MenNewDO menNew)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menNew);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		if(menNew.getStatus()!=null){
			stringObjectMap.put("status",menNew.getStatus()+"");
		}

		List<MenNewDO> list = menNewService.likeList(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出活动专区列表
	 */
	@PreAuthorize("@ss.hasPermi('men:menNew:export')")
	@Log(title = "活动专区", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MenNewDO menNew)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menNew);
		List<MenNewDO> list = menNewService.list(stringObjectMap);
		ExcelUtil<MenNewDO> util = new ExcelUtil<MenNewDO>(MenNewDO.class);
		return util.exportExcel(list, "menNew");
	}

	/**
	 * 获取活动专区详细信息
	 */
	@PreAuthorize("@ss.hasPermi('men:menNew:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(menNewService.get(id));
	}

	/**
	 * 新增活动专区
	 */
	@PreAuthorize("@ss.hasPermi('men:menNew:add')")
	@Log(title = "活动专区", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MenNewDO menNew)
	{
		menNew.setId(IdUtil.simpleUUID());
		menNew.setDelFlag(0);
		menNew.setCaAt(new Date());
		return toAjax(menNewService.save(menNew));
	}

	/**
	 * 修改活动专区
	 */
	@PreAuthorize("@ss.hasPermi('men:menNew:edit')")
	@Log(title = "活动专区", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MenNewDO menNew)
	{
		menNew.setUpAt(new Date());
		return toAjax(menNewService.update(menNew));
	}

	/**
	 * 删除活动专区
	 */
	@PreAuthorize("@ss.hasPermi('men:menNew:remove')")
	@Log(title = "活动专区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MenNewDO menNew = menNewService.get(string);
				menNew.setDelFlag(1);
				menNew.setUpAt(new Date());
				menNewService.update(menNew);
			}
		}
		return toAjax(ids.length);
		//return toAjax(menNewService.deleteMenNewByIds(ids));
	}
}
